package cn.stronglink.asset.module.basicdata.assetType.controller;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import com.baomidou.mybatisplus.plugins.Page;

import cn.stronglink.asset.model.AssetType;
import cn.stronglink.asset.module.basicdata.assetType.service.AssetTypeService;
import cn.stronglink.asset.module.basicdata.assetType.vo.AssetTypeTreeNodeVo;
import cn.stronglink.asset.module.sys.department.vo.DepartmentTreeNodeVo;
import cn.stronglink.suite.core.base.AbstractController;
import cn.stronglink.suite.core.config.Resources;
import cn.stronglink.suite.core.log.annotation.OperateLog;
import cn.stronglink.suite.core.log.annotation.OperateLog.OpType;
import cn.stronglink.suite.core.support.HttpCode;

@Controller
@RequestMapping(value = "basic/assetType")
public class AssetTypeController extends AbstractController {

	@Autowired
	private AssetTypeService assetTypeService;

	@RequestMapping(value = "queryAssetType")
	public ResponseEntity<ModelMap> queryAssetType(ModelMap modelMap, HttpServletRequest request,
			HttpServletResponse response) {
		List<AssetTypeTreeNodeVo> vo = assetTypeService.getTree();
		return setSuccessModelMap(modelMap, vo);
	}
	
	@RequestMapping(value = "queryAllList")
	public ResponseEntity<ModelMap> queryAllList(ModelMap modelMap, HttpServletRequest request,
			HttpServletResponse response) {
		List<AssetTypeTreeNodeVo> vo = assetTypeService.queryAllList();
		return setSuccessModelMap(modelMap, vo);
	}

	@RequestMapping(value = "queryAssetTypeByPid")
	public ResponseEntity<ModelMap> queryAssetTypeByPid(ModelMap modelMap, HttpServletRequest request,
			HttpServletResponse response, @RequestBody Map<String, Object> params) {		
		Long pid = null;
		if(params.get("pid") != null){
			pid = Long.valueOf(params.get("pid").toString());
		}	
		String name = params.get("name").toString();
		@SuppressWarnings("unchecked")
		Page<AssetType> page = (Page<AssetType>) super.getPage(params);
		Page<AssetType> data = assetTypeService.queryAssetTypeByPid(page, pid, name);
		return setSuccessModelMap(modelMap, data);
	}
	
	@OperateLog(module = "资产分类",desc="添加资产分类信息", type = OpType.ADD)
	@RequestMapping(value = "create")
	public ResponseEntity<ModelMap> create(ModelMap modelMap, HttpServletRequest request,
			HttpServletResponse response,@RequestBody AssetType assetType) {
		assetTypeService.insertAssetType(assetType);
		return setSuccessModelMap(modelMap, null);
	}
	
	@OperateLog(module = "资产分类",desc="修改资产分类信息", type = OpType.UPDATE)
	@RequestMapping(value = "edit")
	public ResponseEntity<ModelMap> edit(ModelMap modelMap, HttpServletRequest request,
			HttpServletResponse response,@RequestBody AssetType assetType) {
		assetTypeService.insertAssetType(assetType);
		return setSuccessModelMap(modelMap, null);
	}
	
	@OperateLog(module = "资产分类",desc="删除资产分类", type = OpType.DEL)
	@RequestMapping(value = "remove")
	public ResponseEntity<ModelMap> remove(ModelMap modelMap, HttpServletRequest request,
			HttpServletResponse response,@RequestBody Long id) {
		int count = assetTypeService.getChildAssetTypeCount(id);
		if(count > 0){		
			return setModelMap(modelMap, HttpCode.CONFLICT, Resources.getMessage("HaveChildAssetType"));
		}
		int isUsed =assetTypeService.getIsUsed(id);
		if(isUsed > 0){		
			return setModelMap(modelMap, HttpCode.CONFLICT, Resources.getMessage("IsUsedAssetType"));
		}
		assetTypeService.removeAssetType(id);
		return setSuccessModelMap(modelMap, null);
	}

}
